package com.simple.rpc.core;

/**
 * @author mhh
 * @since 2020/10/21
 */
public class RpcServiceProxyFactory {

    public static RpcServiceProxy getProxy(Class<?> serviceClass) {
        RpcServiceProxy rpcServiceProxy = new RpcServiceProxy();
        try {
            rpcServiceProxy.setServiceInstance(serviceClass.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return rpcServiceProxy;
    }

    public static RpcServiceInvoker getInvoker(RpcServiceProxy rpcServiceProxy, Class<?> serviceClass) {
        return serviceInvokeMetaData -> {
            if (rpcServiceProxy != null) {
                return rpcServiceProxy.doInvoke(serviceInvokeMetaData);
            }
            RpcServiceProxy proxy = getProxy(serviceClass);
            return proxy.doInvoke(serviceInvokeMetaData);
        };
    }
}
